layui.use(['form','treeSelect','layer','upload','laydate','layedit'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    var treeSelect = layui.treeSelect;




    //回显数据
    var request = GetRequest();
     $.get("/param/getById?id=" + request.id,function (data) {
        $("#id").val(data.id);

        $("#paramName").val(data.paramName);


         $.ajax({
             //根据parentId查对应类别，从而拿到父类结点的名称
             url: '/produceCategory/'+data.categoryId,
             type: 'GET',
             success: function (da) {
                 if(da!=null) {
                     //渲染菜单的数据到表单元素
                     treeSelect.render({
                         elem: '#tree-select',
                         data: '/produceCategory/treelist',
                         type: 'get',
                         placeholder: da.name,// 占位符
                         search: true,// 是否开启搜索功能：true/false，默认false
                         click: function(d){
                             $("#tree-select").val(d.current.id);
                         },
                         success: function () { // 加载完成后的回调函数
                             $("#tree-select").val(da.id);
                         }
                     });

                 }

             }});
    });


    form.on("submit(edit)",function(data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        // 实际使用时的提交信息
        $.post("/param/edit",{
            id : $("#id").val(),
            title : $("#paramName").val(),
            categoryId :  $("#tree-select").val(),


        },function(res){
            //
        })
        setTimeout(function(){
            top.layer.close(index);
            top.layer.msg("修改成功！");
            layer.closeAll("iframe");
            //刷新父页面
            parent.location.reload();
        },2000);
        return false;
    })


    //获取路径中的参数
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }


});